package com.alibaba.citrus.cr.sales.plan.facade.service.setting;

import com.alibaba.citrus.cr.common.annotation.FacadeInvoker;
import com.alibaba.citrus.cr.sales.plan.api.setting.NotificationConfigWriteService;
import com.alibaba.citrus.cr.sales.plan.dto.api.request.NotificationConfigQueryRequest;
import com.alibaba.citrus.cr.sales.plan.functions.salesplan.NotificationEntity;
import com.alibaba.citrus.cr.sales.plan.repository.NotificationConfigRepository;
import com.alibaba.citrus.cr.sales.plan.sdo.NotificationConfigSDO;
import com.alibaba.lattice2.epoch.sdk.model.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class NotificationConfigWriteServiceImpl implements NotificationConfigWriteService {

    @Resource
    private NotificationConfigRepository notificationConfigRepository;

    @FacadeInvoker
    @Override
    public Result<String> createNotificationConfig(NotificationConfigSDO notificationConfigSDO) {
        NotificationEntity notificationEntity = NotificationEntity.of(notificationConfigSDO);
        String lastInsertId = notificationEntity.doCreate();
        return Result.success(lastInsertId);
    }

    @FacadeInvoker
    @Override
    public Result<Boolean> editAddressee(NotificationConfigSDO notificationConfigSDO) {
        NotificationEntity notificationEntity = NotificationEntity.of(notificationConfigSDO);
        return Result.success(notificationEntity.doEditRecipient());
    }

    @FacadeInvoker
    @Override
    public Result<Boolean> editContent(NotificationConfigSDO notificationConfigSDO) {
        NotificationEntity notificationEntity = NotificationEntity.of(notificationConfigSDO);
        return Result.success(notificationEntity.doEditContent());
    }

    @FacadeInvoker
    @Override
    public Result<Integer> editNotificationConfigState(NotificationConfigSDO notificationConfigSDO) {
        NotificationConfigQueryRequest configQueryRequest = new NotificationConfigQueryRequest();
        configQueryRequest.setId(String.valueOf(notificationConfigSDO.getId()));
        NotificationConfigSDO byId = notificationConfigRepository.queryNotificationConfig(configQueryRequest);
        if (notificationConfigSDO.getStatus() != null) {
            byId.setStatus(notificationConfigSDO.getStatus());
        }
        NotificationEntity notificationEntity = NotificationEntity.of(byId);
        Integer count = notificationEntity.doEditNotificationConfigState();
        return Result.success(count);
    }
}
